﻿(function($) {

    function Settings() {
        this.nextshowall = false;
        this.peramount = 5;
        this.loadedamount = 0;
        this.total = 0;
        this.last = 0;
        this.amount = 20;
        this.template = ".single_item";
        this.trigger = ".get_more";
        this.itemdatabind = null;
        this.callback = null;
        this.parent = null;
        this.elementAppend = true;
        this.autoload = true;
        this.address = "";
        this.totalCountAddress = ""
    }
    var methods = {
        init_trigger: function() {
            var self = this;
            var settings = self.data("settings");
            var trigger = self.find(settings.trigger);
            $.CallSelfAjaxMethod(settings.totalCountAddress, { subjectID: settings.objectID }, function(data) {
                if (data.d) {
                    settings.total = data.d;
                    trigger.find(".total").text(data.d);
                    trigger.find(".rest").text(settings.total - settings.last);
                }
            });
        },
        setting: function(params) {
            var settings = this.data("settings");
            if (params) {
                $.extend(settings, params);
            }
        },
        reset: function(options) {
            return this.each(function() {
                var self = $(this);
                var settings = self.data("settings");
                if (options) {
                    $.extend(settings, options);
                }
                self.more("clear");
                self.more("init_trigger");
                self.more('get_data');
            })
        },
        init: function(options) {
            return this.each(function() {
                var self = $(this);
                var settings = new Settings();
                if (options) {
                    $.extend(settings, options);
                }
                self.data("settings", settings);
                var template = self.find(settings.template);
                self.data("template", template);
                template.remove();
                self.data("target", self.find(".more_container"));
                var trigger = self.find(settings.trigger);
                trigger.bind('click.more', function() { self.more("get_data", settings.nextshowall); });
                self.more("init_trigger");
                if (settings.autoload) {
                    self.more("get_data");
                }
                else {
                    trigger.show();
                }

            })
        },
        clear: function(params) {
            return this.each(function() {
                var self = $(this);
                var settings = self.data("settings");
                self.find(settings.template).remove();

                if (params) {
                    $.extend(settings, params);
                }
                settings.last = 0;
                settings.total = 0;
                settings.loadedamount = 0;
                self.find(settings.trigger).hide();
            });
        },
        append_elements: function(data) {
            var self = this;
            var settings = self.data("settings");
            var root = self.data("target");
            if (data.length > 0) {
                if (data) {
                    $(data).each(function() {
                        var t = self.data("template");
                        $.each(this, function(key, value) {
                            if (t.find('.' + key)) {
                                if (t.find('.' + key).attr("html") == "true")
                                    t.find('.' + key).html(value);
                                else
                                    t.find('.' + key).text(value);
                            }
                        })
                        var element = t.clone();
                        root.append(element);
                        element.show();
                        element.data("itemdata", this);
                        if (settings.itemdatabind != null) {
                            settings.itemdatabind(element, this, settings.parent);
                        }
                        settings.last++;
                    })
                }
            }
        },
        add_elements: function(data) {
            var self = this;
            var settings = self.data("settings");
            var root = self.data("target");
            if (data.length > 0) {
                if (data) {
                    $(data).each(function() {
                        var t = self.data("template");
                        $.each(this, function(key, value) {
                            if (t.find('.' + key)) {
                                if (t.find('.' + key).attr("html") == "true")
                                    t.find('.' + key).html(value);
                                else
                                    t.find('.' + key).text(value);
                            }
                        })
                        var element = t.clone();
                        root.prepend(element);
                        element.show();
                        element.data("itemdata", this);
                        if (settings.itemdatabind != null) {
                            settings.itemdatabind(element, this, settings.parent);
                        }
                        settings.last++;
                    })
                }
            }
        },
        get_data: function(showall) {
            var self = this;
            var settings = self.data("settings");
            var params = $.extend({
                objectID: settings.objectID,
                last: settings.last,
                amount: settings.peramount
            }, settings.params);

            var trigger = self.find(settings.trigger);
            if (trigger.next(".loading").length == 0) {
                trigger.after($("<img alt='' class='loading' src='/images/loading.gif'/>"));
            }

            if (showall) {
                if (settings.loadedamount >= settings.total) {
                    settings.loadedamount = 0;
                    trigger.next(".loading").remove();
                    return;
                }
            }
            else {
                if (settings.loadedamount >= settings.amount) {
                    settings.loadedamount = 0;
                    trigger.find(".rest").text(settings.total - settings.last);
                    trigger.show();
                    trigger.next(".loading").remove();
                    return;
                }
            }

            $.CallSelfAjaxMethod(settings.address, params, function(data) {
                if (data.d && data.d.length > 0) {
                    settings.loadedamount += data.d.length;
                    if (settings.elementAppend)
                        self.more("append_elements", data.d);
                    else
                        self.more("add_elements", data.d);

                    if (settings.callback != null) {
                        settings.callback(params, data.d);
                    }
                }
                if (settings.last >= settings.total) {
                    trigger.hide();
                    trigger.next(".loading").remove();
                }
                else {
                    //trigger.show();
                    self.more("get_data", showall);
                }

            });
        }
    };
    $.fn.more = function(method) {
        if (methods[method])
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        else if (typeof method == 'object' || !method)
            return methods.init.apply(this, arguments);
        else $.error('Method ' + method + ' does not exist!');
    }
})(jQuery)